### getSetREADY table ##

## Import ##

import sys
from PyQt4 import QtCore, QtGui
from table_ui import Ui_frmTable
import os
import commands
import re

## Rascunho para o import automatizado (falta retirar a extensao .py e remover files irrelevantes ##

##varlines = commands.getoutput('ls -1 gsr/ | wc -l')
##importname = """commands.getoutput("ls gsr/ | awk 1 | sed -n '"""
##for k in range(1,int(varlines)):
    ##importname_ = importname + str(k) + """p' | sed -e 's/.py/ /g'")"""
    ##name =  eval(importname_)
    ##importstatement = "from gsr import " + str(name)
    ##exec(importstatement)

## array bidimensional de aplicacoes
apps = [['amsn', 'sucka','0','Communication'], ['kopete', 'rula','1','KDE'], ['gaim', 'pouco','0','Communication']]

class getsetreadytable(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_frmTable()
        self.ui.setupUi(self)
        self.boot()
        self.tableupdate()
        self.slots()
        self.filterparse()
    
    def boot(self):
        self.ui.tableWidget.verticalHeader().hide()
        self.ui.listWidget.setCurrentRow(0)
   
    def filterparse(self):
        self.ui.listFilter.clear()
        for h in range(0,len(apps)):
            if self.ui.comboFilter.currentText() == '':
                item = QtGui.QListWidgetItem(self.ui.listFilter)
                item.setText(apps[h][0])
            else:
                try:
                    print self.ui.comboFilter.currentText()
                    re.search(str(self.ui.comboFilter.currentText()),str(apps[h][0])).span()
                    item = QtGui.QListWidgetItem(self.ui.listFilter)
                    item.setText(apps[h][0])
                except:
                    debug = apps[h][0] + ' not filterable'
                    print debug

    def action(self):
        print 'hi'

    def tableupdate(self):
        ## Reconfigurar Tabela
        self.ui.tableWidget.clear()
        headerItem = QtGui.QTableWidgetItem()
        headerItem.setText("State")
        self.ui.tableWidget.setHorizontalHeaderItem(0,headerItem)
        headerItem1 = QtGui.QTableWidgetItem()
        headerItem1.setText("Name")
        self.ui.tableWidget.setHorizontalHeaderItem(1,headerItem1)
        headerItem2 = QtGui.QTableWidgetItem()
        headerItem2.setText("Summary")
        self.ui.tableWidget.setHorizontalHeaderItem(2,headerItem2)
        icon = QtGui.QTableWidgetItem()
        ##
        a = 0
        h=0
        for k in apps:
            ##state
            icon = QtGui.QTableWidgetItem()
            if self.ui.listWidget.item(self.ui.listWidget.currentRow()).text() != 'All':
                if k[3] == self.ui.listWidget.item(self.ui.listWidget.currentRow()).text():
                    if k[2] == '0':
                        icon.setText('Available')
                        icon.setIcon(QtGui.QIcon("pixmaps/white.png"))
                    elif k[2] == '1':
                        icon.setText('Installed')
                        icon.setIcon(QtGui.QIcon("pixmaps/green.png"))
                    elif k[2] == '2':
                        icon.setText('Installing')
                        icon.setIcon(QtGui.QIcon("pixmaps/yellow.png"))
                    elif k[2] == '3':
                        icon.setText('Removing')
                        icon.setIcon(QtGui.QIcon("pixmaps/red.png"))
                    self.ui.tableWidget.setItem(h,0,icon)
                    ##name
                    name = QtGui.QTableWidgetItem()
                    name.setText(k[0])
                    self.ui.tableWidget.setItem(h,1,name)
                    ##summary
                    summary = QtGui.QTableWidgetItem()
                    summary.setText(k[1])
                    self.ui.tableWidget.setItem(h,2,summary)
                    h = h +1
            else:
                self.ui.tableWidget.setRowCount(len(apps))
                if k[2] == '0':
                    icon.setText('Available')
                    icon.setIcon(QtGui.QIcon("pixmaps/white.png"))
                elif k[2] == '1':
                    icon.setText('Installed')
                    icon.setIcon(QtGui.QIcon("pixmaps/green.png"))
                elif k[2] == '2':
                    icon.setText('Installing')
                    icon.setIcon(QtGui.QIcon("pixmaps/yellow.png"))
                elif k[2] == '3':
                    icon.setText('Removing')
                    icon.setIcon(QtGui.QIcon("pixmaps/red.png"))
                self.ui.tableWidget.setItem(a,0,icon)
                ##name
                name = QtGui.QTableWidgetItem()
                name.setText(k[0])
                self.ui.tableWidget.setItem(a,1,name)
                ##summary
                summary = QtGui.QTableWidgetItem()
                summary.setText(k[1])
                self.ui.tableWidget.setItem(a,2,summary)
                a = a+1
    
    def slots(self):
        QtCore.QObject.connect(self.ui.tableWidget,QtCore.SIGNAL("cellDoubleClicked(int,int)"), self._tableclick)
        QtCore.QObject.connect(self.ui.listWidget,QtCore.SIGNAL("currentRowChanged(int)"), self._listclick)
        QtCore.QObject.connect(self.ui.comboFilter,QtCore.SIGNAL("textChanged(QString)"), self._filter)
    
    def _tableclick(self):
        selection = self.ui.tableWidget.currentRow()
        for k in range(0,len(apps)):
            if self.ui.tableWidget.item(selection,1).text() == apps[k][0]:
                if apps[k][2] == '0':
                    apps[k][2] = '2'
                elif apps[k][2] == '1':
                    apps[k][2] = '3'
                elif apps[k][2] == '2':
                    apps[k][2] = '0'
                elif apps[k][2] == '3':
                    apps[k][2] = '1'
        self.tableupdate()
    
    def _listclick(self):
        self.tableupdate()
    
    def _filter(self):
        self.filterparse()

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    gsrcore = getsetreadytable()
    gsrcore.show()
    """tray = QtGui.QSystemTrayIcon()
    tray.isSystemTrayAvailable()
    tray.setIcon(QtGui.QIcon("pixmaps/green.png"))
    tray.setToolTip('getSetREADY!')
    menu = QtGui.QMenu("teste")
    menu.addAction('yellow moto')
    tray.setContextMenu(menu)
    tray.show()"""
    sys.exit(app.exec_())
